package com.yanzhu.service.mapper;

import com.yanzhu.service.model.Upvote;
import com.yanzhu.service.model.mix.LikeInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;


/**
 * @Author: 方炎柱
 * @Description:
 * @Date: Created in 21:46 2018/1/21
 * @Modified:
 */
@Mapper
public interface LikeMapper {
    @Insert("insert into upvote(uid, vid, like_time) values(#{uid}, #{vid}, #{like_time})")
    @SelectKey(statement = "SELECT LAST_INSERT_ID()", keyProperty = "id", before = false, resultType = long.class)
    int insert(Upvote like);

    @Select("select * from upvote where uid = #{uid} order by like_time desc")
    List<Upvote> queryLikeList(@Param("uid") long uid);

    @Select("select count(*) from upvote where vid = #{vid}")
    int queryVideoLikeCount(@Param("vid") long vid);

    @Select("select count(*) from upvote where vid = #{vid} and uid = #{uid}")
    int queryLikeCount(@Param("vid") long vid, @Param("uid") long uid);

    @Select("select count(distinct uid) from upvote where vid = #{vid} ")
    int queryVideoLikeUserCount(@Param("vid") long vid);

    @Select("select u.avatar" +
            " from upvote l" +
            " left join user u on u.id = l.uid" +
            " where l.vid = #{vid}" +
            " group by l.uid" +
            " order by count(l.uid) desc" +
            " limit 0, #{size}")
    List<String> queryVideoLikeUserAvatar(@Param("vid") long vid, @Param("size") int size);

    @Select("select l.uid, u.nickname, u.avatar count(l.uid) as speed" +
            " from upvote l" +
            " left join user u on u.id = l.uid" +
            " where vid = #{vid} and l.like_time <= #{like_time}" +
            " group by l.uid" +
            " order by count(l.uid) desc")
    List<LikeInfo> queryVideoLikeSpedd(@Param("vid") long vid, @Param("like_time") int like_time);

    @Delete("delete from upvote where id = #{id}")
    int deleteLikeById(@Param("id") long id);

    @Delete("delete from upvote where uid=#{uid}")
    int deleteLikeAll(@Param("uid") long uid);
}
